package com.fqgj.sentry.biz.rm;

import com.fqgj.report.statistics.entity.BiGradeStatisticEntity;
import com.fqgj.sentry.biz.rm.vo.CategoryVo;
import com.fqgj.sentry.biz.rm.vo.ScoreComboVo;
import com.fqgj.sentry.biz.rm.vo.ScoreModelCharVo;
import com.fqgj.sentry.biz.rm.vo.ScoreModelTableVo;
import com.fqgj.sentry.common.utils.Arith;
import com.fqgj.sentry.controller.rm.request.QueryComboRequest;
import com.fqgj.sentry.controller.rm.request.ScoreQueryTermsRequest;
import com.fqgj.sentry.manage.entity.McApplicationEntity;
import com.fqgj.sentry.manage.service.ApplicationService;
import com.fqgj.sentry.policy.client.PolicyExecutorService;
import com.fqgj.sentry.policy.client.PolicyService;
import com.fqgj.sentry.policy.client.PolicySetService;
import com.fqgj.sentry.policy.entity.PcExecutorEntity;
import com.fqgj.sentry.policy.entity.PcPolicyEntity;
import com.fqgj.sentry.policy.entity.PcPolicySetsEntity;
import com.fqgj.sentry.statistics.service.GradeStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-10-16 下午2:52
 */

@Component
public class ScoreModelBusiness {

    @Autowired
    private ApplicationService applicationService;
    @Autowired
    private PolicySetService policySetService;
    @Autowired
    private PolicyExecutorService policyExecutorService;
    @Autowired
    private PolicyService policyService;

    @Autowired
    private GradeStatisticsService gradeStatisticsService;

    public List<ScoreModelTableVo> getScoreModelTable(ScoreQueryTermsRequest termsRequest) {

        List<ScoreModelTableVo> scoreModelTableVos = new LinkedList<>();

        List<BiGradeStatisticEntity> entityList = gradeStatisticsService.selectRiskGradeStatistic(termsRequest.getStartDate(),
                termsRequest.getEndDate(), termsRequest.getEventId(), termsRequest.getExecutorType(), termsRequest.getSource(), termsRequest.getBorrowType(), termsRequest.getScoreField());


        ScoreModelTableVo totalScoreVo = new ScoreModelTableVo()
                .setApplyNum(0L)
                .setModelScore("总体")
                .setRejectNum(0L)
                .setNoRejectNum(0L)
                .setNoRejectRate(0.0)
                .setOnOverdueRate(0.0)
                .setBackRate(0.0)
                .setOneDayOverdueRate(0.0)
                .setFifteenOverdueBackRate(0.0)
                .setFifteenDayOverdueRate(0.0);

        Long totalApplyCnt = 0L;
        //未命中规则累计数
        Long totalNotHitCnt = 0L;
        Long totalHitCnt = 0L;
        //总的放款数
        Long totalPaidCnt = 0L;
        //总的在逾数
        Long totalOverdueCnt = 0L;
        //总的d0应还数
        Long totalBackCnt = 0L;
        Long totalOneDayOverdueCnt = 0L;
        //总的d15应还数
        Long totalFifteenBackCnt = 0L;
        //总的d15逾期数
        Long totalFifteenOverdueCnt = 0L;

        for (BiGradeStatisticEntity entity : entityList) {
            totalNotHitCnt += entity.getNotHitCnt();
            totalHitCnt += entity.getHitCnt();
            totalApplyCnt += entity.getApplyCnt();
            totalPaidCnt += entity.getPaidCnt();
            totalOverdueCnt += entity.getOverdueCnt();
            totalBackCnt += entity.getOneDayBackCnt();
            totalFifteenBackCnt += entity.getFifteenDayBackCnt();
            totalFifteenOverdueCnt += entity.getFifteenDayOverdueCnt();
            totalOneDayOverdueCnt += entity.getOneDayOverdueCnt();

            ScoreModelTableVo scoreModelTableVo = new ScoreModelTableVo()
                    .setApplyNum(entity.getApplyCnt())
                    .setModelScore(entity.getGrade().toString())
                    .setRejectNum(entity.getHitCnt())
                    .setNoRejectNum(totalNotHitCnt)
                    .setNoRejectRate(0.0)
                    .setOnOverdueRate(0.0)
                    .setBackRate(0.0)
                    .setOneDayOverdueRate(0.0)
                    .setFifteenOverdueBackRate(0.0)
                    .setFifteenDayOverdueRate(0.0);

            //在逾率 首逾率
            if (entity.getOneDayBackCnt() != 0) {
                scoreModelTableVo.setOnOverdueRate(Arith.divide(entity.getOverdueCnt() * 100, entity.getOneDayBackCnt()));
                scoreModelTableVo.setOneDayOverdueRate(Arith.divide(entity.getOneDayOverdueCnt() * 100, entity.getOneDayBackCnt()));
            }
            //首逾应还率 d15应还率
            if (entity.getPaidCnt() != 0) {
                scoreModelTableVo.setBackRate(Arith.divide(entity.getOneDayBackCnt() * 100, entity.getPaidCnt()));
                scoreModelTableVo.setFifteenOverdueBackRate(Arith.divide(entity.getFifteenDayBackCnt() * 100, entity.getPaidCnt()));
            }
            if (entity.getFifteenDayBackCnt() != 0) {
                scoreModelTableVo.setFifteenDayOverdueRate(Arith.divide(entity.getFifteenDayOverdueCnt() * 100, entity.getFifteenDayBackCnt()));

            }
            scoreModelTableVos.add(scoreModelTableVo);
        }

        for (ScoreModelTableVo scoreModelTableVo : scoreModelTableVos) {
            //累计未命中拒绝率
            if (totalApplyCnt != 0) {
                scoreModelTableVo.setNoRejectRate(Arith.divide(scoreModelTableVo.getNoRejectNum() * 100, totalApplyCnt));
            }
        }


        totalScoreVo.setApplyNum(totalApplyCnt)
                .setRejectNum(totalHitCnt)
                .setNoRejectNum(totalNotHitCnt);
        if (totalApplyCnt != 0) {
            totalScoreVo.setNoRejectRate(Arith.divide(totalNotHitCnt * 100, totalApplyCnt));
        }
        if (totalBackCnt != 0) {
            totalScoreVo.setOnOverdueRate(Arith.divide(totalOverdueCnt * 100, totalBackCnt));
            totalScoreVo.setOneDayOverdueRate(Arith.divide(totalOneDayOverdueCnt * 100, totalBackCnt));

        }
        if (totalPaidCnt != 0) {
            totalScoreVo.setBackRate(Arith.divide(totalBackCnt * 100, totalPaidCnt));
            totalScoreVo.setFifteenOverdueBackRate(Arith.divide(totalFifteenBackCnt * 100, totalPaidCnt));
        }
        if (totalFifteenBackCnt != 0) {
            totalScoreVo.setFifteenDayOverdueRate(Arith.divide(totalFifteenOverdueCnt * 100, totalFifteenBackCnt));
        }
        scoreModelTableVos.add(totalScoreVo);

        return scoreModelTableVos;
    }

    public ScoreModelCharVo getOverdueStatisticChart(ScoreQueryTermsRequest termsRequest) {

        ScoreModelCharVo scoreModelCharVo = new ScoreModelCharVo();

        List<BiGradeStatisticEntity> entityList = gradeStatisticsService.selectRiskGradeStatistic(termsRequest.getStartDate(),
                termsRequest.getEndDate(), termsRequest.getEventId(), termsRequest.getExecutorType(), termsRequest.getSource(), termsRequest.getBorrowType(), termsRequest.getScoreField());


        List<Long> xAxis = new ArrayList<>();
        List<CategoryVo<Double>> columnSeries = new LinkedList<>();
        List<CategoryVo<Double>> lineSeries = new LinkedList<>();

        if (entityList.size() != 0) {

            Long totalApplyCnt = 0L;
            //未命中规则累计数
            Long totalNotHitCnt = 0L;
            CategoryVo<Double> cumulativeCategory = new CategoryVo<>();
            cumulativeCategory.setName("累计占比");
            List<Double> cumulativeList = new LinkedList<>();


            CategoryVo<Double> oneDayOverdueRate = new CategoryVo<>();
            CategoryVo<Double> onOverdueRate = new CategoryVo<>();
            CategoryVo<Double> fifteenDayOverdueRate = new CategoryVo<>();
            onOverdueRate.setName("在逾率(个数%)");
            oneDayOverdueRate.setName("首逾率(个数%)");
            fifteenDayOverdueRate.setName("d15逾期率(个数%)");

            List<Double> onOverdueList = new LinkedList<>();
            List<Double> oneDayOverdueList = new LinkedList<>();
            List<Double> fifteenDayList = new LinkedList<>();

            for (BiGradeStatisticEntity biGradeStatisticEntity : entityList) {
                totalApplyCnt += biGradeStatisticEntity.getApplyCnt();
            }
            for (BiGradeStatisticEntity entity : entityList) {
                xAxis.add(entity.getGrade());
                totalNotHitCnt += entity.getNotHitCnt();
                if (totalApplyCnt != 0) {
                    cumulativeList.add(Arith.divide(totalNotHitCnt * 100, totalApplyCnt));
                } else {
                    cumulativeList.add(0.0);
                }
                //在逾率 首逾率
                if (entity.getOneDayBackCnt() != 0) {
                    onOverdueList.add(Arith.divide(entity.getOverdueCnt() * 100, entity.getOneDayBackCnt()));
                    oneDayOverdueList.add(Arith.divide(entity.getOneDayOverdueCnt() * 100, entity.getOneDayBackCnt()));
                } else {
                    onOverdueList.add(0.0);
                    oneDayOverdueList.add(0.0);
                }
                if (entity.getFifteenDayBackCnt() != 0) {
                    fifteenDayList.add(Arith.divide(entity.getFifteenDayOverdueCnt() * 100, entity.getFifteenDayBackCnt()));
                } else {
                    fifteenDayList.add(0.0);
                }
            }

            cumulativeCategory.setData(cumulativeList);
            columnSeries.add(cumulativeCategory);
            scoreModelCharVo.setColumnSeries(columnSeries);
            onOverdueRate.setData(onOverdueList);
            oneDayOverdueRate.setData(oneDayOverdueList);
            fifteenDayOverdueRate.setData(fifteenDayList);

            lineSeries.add(onOverdueRate);
            lineSeries.add(oneDayOverdueRate);
            lineSeries.add(fifteenDayOverdueRate);
            scoreModelCharVo.setLineSeries(lineSeries);
        }

        scoreModelCharVo.setxAxis(xAxis);
        scoreModelCharVo.setColumnSeries(columnSeries);
        scoreModelCharVo.setLineSeries(lineSeries);

        return scoreModelCharVo;
    }

    public List<ScoreComboVo> getScoreCombo(String appCode, Long merchantId, QueryComboRequest queryComboRequest) {

        McApplicationEntity applicationEntity = applicationService.selectByMerchantIdAndAppCode(merchantId, appCode);
        PcPolicySetsEntity pcPolicySetsEntity = policySetService.selectByAppIdAndEventId(applicationEntity.getId(), queryComboRequest.getEventId());
        List<ScoreComboVo> scoreComboVos = new ArrayList<>();
        if (pcPolicySetsEntity == null) {
            return scoreComboVos;
        }
        String versionType = queryComboRequest.getExecutorType();
        PcExecutorEntity pcExecutorEntity = policyExecutorService.selectPcExecutorCurrent(pcPolicySetsEntity.getId(), Integer.valueOf(versionType), null);
        if (pcExecutorEntity != null) {
            List<PcPolicyEntity> pcPolicyEntities = policyService.selectPolicyByExecutorId(pcExecutorEntity.getId());
            for (PcPolicyEntity entity : pcPolicyEntities) {
                if (entity.getType() == 2) {
                    ScoreComboVo scoreComboVo = new ScoreComboVo()
                            .setScoreField(entity.getColumnCode())
                            .setScoreValue(entity.getColumnCode());
                    scoreComboVos.add(scoreComboVo);
                }
            }
        }
        return scoreComboVos;

    }
}
